<template>
  <div class="app-container">
    <!-- 标题 -->
    <p style="font: 20px Extra large">医院设置列表</p>
    <!-- 条件查询 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="searchObj.hosname" placeholder="医院名称" />
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchObj.hoscode" placeholder="医院编号" />
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="getHospList()"
        >查询</el-button
      >
    </el-form>
    <!--批量删除-->
    <div>
      <el-button type="danger" size="mini" @click="deleteBatch()"
        >批量删除</el-button
      >
    </div>
    <!-- 医院设置列表展示 -->
    <el-table
      :data="list"
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <!-- 添加复选框 -->
      <el-table-column type="selection" width="55" />
      <!-- 相关数据显示 -->
      <el-table-column type="index" width="50" label="序列" />
      <el-table-column prop="hosname" label="医院名称" />
      <el-table-column prop="hoscode" label="医院编号" />
      <el-table-column prop="apiUrl" label="api基础路径" width="200" />
      <el-table-column prop="contactsName" label="联系人姓名" />
      <el-table-column prop="contactsPhone" label="联系人手机" />
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? "可用" : "不可用" }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="280" align="center">
        <!-- 单个记录删除 -->
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="deleteHospSetById(scope.row.id)"
          ></el-button>
          <!-- 改变医院状态 -->
          <el-button
            v-if="scope.row.status == 1"
            type="primary"
            size="mini"
            icon="el-icon-delete"
            @click="updateStatus(scope.row.id, 0)"
            >锁定</el-button
          >
          <el-button
            v-if="scope.row.status == 0"
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="updateStatus(scope.row.id, 1)"
            >取消锁定</el-button
          >
          <!-- 修改医院设置-->
          <router-link :to="'/hospSet/edit/' + scope.row.id">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
            ></el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="current"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="getHospList"
    />
  </div>
</template>

<script>
import hospset from "@/api/hospset";
//引入js利用axois请求
export default {
  //定义变量和数据
  data() {
    return {
      current: 1,
      limit: 3,
      searchObj: {},
      list: [],
      total: 0,
      dataSelection: [],
    };
  },
  //页面渲染前执行
  created() {
    this.getHospList();
  },
  //调用相关方法
  methods: {
    //一、查询及分页
    getHospList(page = 1) {
      this.current = page;
      // alert("传入的页数为："+page)
      hospset
        .getList(this.current, this.limit, this.searchObj)
        .then((response) => {
          this.list = response.data.records;
          this.total = response.data.total;
        })
        .catch((error) => {
          console.log(error);
        });
    },

    //二、删除指定医院设置
    deleteHospSetById(id) {
      //alert("要删除的id: "+id)
      //a.弹框消息提示
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //b.执行通过id删除医院设置
          hospset
            .deleteHospSetById(id)
            .then((response) => {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              //c.更新页面
              this.getHospList(1);
            })
            .catch((error) => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //三、根据id批量删除
    //3.1、复选框状态变化触发
    handleSelectionChange(selection) {
      //将选中的列表赋值
      this.dataSelection = selection;
    },
    //3.2进行批量删除
    deleteBatch() {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        //确认删除后调用接口执行批量删除
        let idList = [];
        let selectData;
        for (let i = 0; i < this.dataSelection.length; i++) {
          selectData = this.dataSelection[i];
          //添加被选中项的id
          idList.push(selectData.id);
        }
        hospset
          .deleteBatchByIds(idList)
          .then((response) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getHospList(1);
          })
          .catch((error) => {
            console.log(error);
          });
      });
    },

    //四、改变医院状态
    updateStatus(id, status) {
      hospset
        .updateStatus(id, status)
        .then((response) => {
          //执行成功刷新当前页面:顺便把当前页传过去
          this.getHospList(this.current);
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
</script>